package com.sinothk.fc.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.goods.entity.GoodsDTO;
import com.sinothk.fc.goods.entity.GoodsEntity;
import com.sinothk.fc.goods.entity.GoodsSearchReq;
import com.sinothk.fc.goods.entity.GoodsVO;
import com.sinothk.fc.goods.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 产品信息
 * @Author: Liangyt
 * @Date: 2021-12-28
 * @Version: V1.0
 */
@Api(tags = "卖家-产品信息")
@RestController
@RequestMapping("/fc/goodsCtr")
@Slf4j
public class GoodsController extends JeecgController<GoodsEntity, IGoodsService> {
    @Autowired
    private IGoodsService goodsInfoService;

    /**
     * 分页列表查询-最新产品
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "分页列表查询-最新产品")
    @ApiOperation(value = "分页列表查询-最新产品", notes = "分页列表查询-最新产品")
    @GetMapping(value = "/listForGoodsNew")
    public Result<?> listForGoodsNew(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();//QueryGenerator.initQueryWrapper(goods, req.getParameterMap());
        queryWrapper.lambda().orderByDesc(GoodsEntity::getCreateTime);
        IPage<GoodsEntity> pageList = goodsInfoService.page(new Page<GoodsEntity>(pageNo, pageSize), queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param goodsEntity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(GoodsEntity goodsEntity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<GoodsEntity> queryWrapper = QueryGenerator.initQueryWrapper(goodsEntity, req.getParameterMap());
        Page<GoodsEntity> page = new Page<GoodsEntity>(pageNo, pageSize);
        IPage<GoodsEntity> pageList = goodsInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 卖家-产品信息-添加或编辑
     *
     * @param goodsReq
     * @return
     */
    @AutoLog(value = "产品信息-添加或编辑")
    @ApiOperation(value = "产品信息-添加或编辑", notes = "产品信息-添加或编辑")
    @PostMapping(value = "/addOrEdit")
    public Result<?> addOrEdit(@RequestBody GoodsEntity goodsReq) throws Exception {
        return Result.OK(goodsInfoService.addOrEdit(goodsReq));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id删除")
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        goodsInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        GoodsEntity goodsEntity = goodsInfoService.getById(id);
        if (goodsEntity == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(goodsEntity);
    }

    @AutoLog(value = "分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/getStoreAllGoodsByState")
    public Result<?> getStoreAllGoodsByState(GoodsSearchReq searchReq,
                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<GoodsVO> pageList = goodsInfoService.getStoreAllGoodsByState(new Page<>(pageNo, pageSize), searchReq);
        return Result.OK(pageList);
    }

    @AutoLog(value = "分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/storeGoodsPageByStoreId")
    public Result<?> storeGoodsPageByStoreId(GoodsSearchReq searchReq, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<GoodsVO> pageList = goodsInfoService.storeGoodsPageByStoreId(new Page<>(pageNo, pageSize), searchReq.getStoreId());
        return Result.OK(pageList);
    }

    @AutoLog(value = "分页-供应商首页推荐")
    @ApiOperation(value = "分页-供应商首页推荐", notes = "分页-供应商首页推荐")
    @GetMapping(value = "/supplierHomePage")
    public Result<?> supplierHomePage(GoodsEntity goodsEntity,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) throws Exception {
        IPage<GoodsEntity> pageList = goodsInfoService.supplierHomePage(new Page<GoodsEntity>(pageNo, pageSize), goodsEntity);
        return Result.OK(pageList);
    }

    @AutoLog(value = "分页-供应商-搜索推荐")
    @ApiOperation(value = "分页-供应商-搜索推荐", notes = "分页-供应商-搜索推荐")
    @GetMapping(value = "/supplierSearchHotPage")
    public Result<?> supplierSearchHotPage(GoodsEntity goodsEntity,
                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<GoodsEntity> pageList = goodsInfoService.supplierSearchHotPage(new Page<>(pageNo, pageSize), goodsEntity);
        return Result.OK(pageList);
    }

    @AutoLog(value = "分页-供应商-关键字搜索")
    @ApiOperation(value = "分页-供应商-关键字搜索", notes = "分页-供应商-关键字搜索")
    @GetMapping(value = "/supplierSearchByKeywordsPage")
    public Result<?> supplierSearchByKeywordsPage(@RequestParam(name = "keywords") String keywords,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<GoodsEntity> pageList = goodsInfoService.supplierSearchByKeywordsPage(new Page<>(pageNo, pageSize), keywords);
        return Result.OK(pageList);
    }

    @AutoLog(value = "展示-商品详情")
    @ApiOperation(value = "展示-商品详情", notes = "展示-商品详情")
    @GetMapping(value = "/goodsShowDetail")
    public Result<GoodsDTO> goodsShowDetail(@RequestParam(name = "goodsId") String goodsId) throws Exception {
        GoodsDTO goodsDTO = goodsInfoService.goodsShowDetail(goodsId);
        return Result.OK(goodsDTO);
    }

    @AutoLog(value = "商品详情-条形码查询")
    @ApiOperation(value = "商品详情-条形码查询", notes = "商品详情-条形码查询")
    @GetMapping(value = "/goodsByBarCode")
    public Result<GoodsEntity> goodsByBarCode(@RequestParam(name = "barCode") String barCode) throws Exception {
        GoodsEntity goods = goodsInfoService.goodsByBarCode(barCode);
        return Result.OK(goods);
    }

    @AutoLog(value = "商品-同步库存")
    @ApiOperation(value = "商品-同步库存", notes = "商品-同步库存")
    @PutMapping(value = "/updateGoodsNumToStockNumByBarcode")
    public Result<String> updateGoodsNumToStockNumByBarcode(@RequestParam(name = "barcode") String barcode) throws Exception {
        goodsInfoService.updateGoodsNumToStockNumByBarcode(barcode);
        return Result.OK("同步成功");
    }
}
